package com.tianji.learning.listener;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 接受退款消息
 */

@Slf4j
@Component
@RequiredArgsConstructor
public class LessonReFundListener {

    private final ILearningLessonService learningLessonService;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "learning.order.refund.queue"),
            exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.ORDER_REFUND_KEY))
    public void listenRefund(OrderBasicDTO orderBasicDTO) {
        log.info("LessonReFundListener 接受到了消息,用户{},退款课程{}", orderBasicDTO.getUserId(), orderBasicDTO.getCourseIds());
        // 1、校验
        if (orderBasicDTO.getUserId() == null || orderBasicDTO.getCourseIds() == null || orderBasicDTO.getCourseIds().size() == 0) {
            // 相当于你消息有问题，但是如果你抛异常的他就会去重试，但是终究是这个消息的问题，你再怎么重试都是不行的
            log.error("LessonReFundListener 接受到了空消息");
            // 注意不要抛异常 这里就直接返回就可以了
            return;
        }

        // 2、调用service 更新课程状态
        learningLessonService.deleteUserLesson(orderBasicDTO.getUserId(), orderBasicDTO.getCourseIds());


    }

}
